package dt.inlee.common.pagination;

import java.util.List;

public class PageBean {

	public int MAX_PAGESIZE = 100;
	
	private int DEFAULT_PAGESIZE = 10;
	
	private int DEFAULT_PAGEINDEX = 1;
	
	private int pageSize = DEFAULT_PAGESIZE;
	
	private int pageIndex = DEFAULT_PAGEINDEX;

	private int totalData;
	
	private int totalPage = 0;
	
	private int startIndex = 0;
	
	private int offset = 0;
	
	private SearchBean params;

	private String strWhere;

	private String descField;

	private boolean isAsc;
	/**
	 * ��ѯ���
	 */
	private List<?> results;

	/**
	 * ��ȡ��ǰҳ����
	 * 
	 * @return
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * ��ȡÿҳ��ʾ���������
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * ��ȡ��ѯ�����
	 * 
	 * @return
	 */
	public SearchBean getParams() {
		if (this.params == null) {
			this.params = new SearchBean();
		}
		return params;
	}

	/**
	 * ��ȡ��ҳ��ѯ���
	 * 
	 * @return
	 */
	public List<?> getResults() {
		return this.results;
	}

	/**
	 * ��ȡ��ݵĿ�ʼ�������ڲ�ѯ
	 * 
	 * @return
	 */
	public int getStartIndex() {
		this.startIndex = (this.pageIndex - 1) * this.pageSize;
		return startIndex;
	}

	/**
	 * ��ȡ�ܵ��������
	 * 
	 * @return
	 */
	public int getTotalData() {
		return totalData;
	}

	/**
	 * ��ȡ��ҳ�����ҳ��
	 * 
	 * @return
	 */
	public int getTotalPage() {
		this.totalPage = this.getTotalData() / this.getPageSize()
				+ (this.getTotalData() % this.getPageSize() == 0 ? 0 : 1);
		return totalPage;
	}

	/**
	 * ҳ������
	 * 
	 * @param offset
	 *            ������
	 */
	public void setOffset(int offset) {
		this.offset = offset;
		int index = (this.offset / pageSize) + 1;
		this.setPageIndex(index);
	}

	/**
	 * ҳ������
	 * 
	 * @param offset
	 *            �ַ�����
	 */
	public void setOffset(String offset) {
		if (offset == null) {
			offset = "0";
		}
		this.setOffset(Integer.valueOf(offset));
	}

	/**
	 * ����ҳ����
	 * 
	 * @param pageIndex
	 */
	public void setPageIndex(int pageIndex) {
		if (pageIndex <= 0) {
			pageIndex = DEFAULT_PAGEINDEX;
		} else if (this.totalPage != 0 && pageIndex > this.totalPage) {
			System.out.println("totalPage " + totalPage);
			pageIndex = this.totalPage;
		}
		this.pageIndex = pageIndex;

		// System.out.println("pageIndex " + pageIndex);
	}

	/**
	 * ����ÿҳ��ʾ�������
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		if (pageSize <= 0) {
			pageSize = DEFAULT_PAGESIZE;
		} else if (pageSize > MAX_PAGESIZE) {
			pageSize = MAX_PAGESIZE;
		}
		this.pageSize = pageSize;
	}

	/**
	 * ���ò�ѯ�����
	 * 
	 * @param params
	 */
	public void setParams(SearchBean params) {
		this.params = params;
	}

	/**
	 * ���ò�ѯ���
	 * 
	 * @param results
	 */
	public void setResults(List<?> results) {
		this.results = results;
	}

	/**
	 * �����������
	 * 
	 * @param totalData
	 */
	public void setTotalData(int totalData) {
		this.totalData = totalData;
	}

	public String getStrWhere() {
		return strWhere;
	}

	public void setStrWhere(String strWhere) {
		this.strWhere = strWhere;
	}

	public String getDescField() {
		if (descField == null)
			return "";
		return descField;
	}

	public void setDescField(String descField) {
		this.descField = descField;
	}

	public boolean isAsc() {
		return isAsc;
	}

	public void setAsc(boolean isAsc) {
		this.isAsc = isAsc;
	}

}
